package demo.hdz.testui;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;

/**
 * @author hdz
 * @date 2018/07/04
 */
public class AnimationActivity extends AppCompatActivity {

    private ImageView imageConnecting;
    private Animation mAnimation;

    private ImageView imageLoading0;
    private Animation mAnimation0;
    private ImageView imageLoading1;
    private Animation mAnimation1;

    private AnimationDrawable mAnimationWait;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);

        ImageView waitImageView = (ImageView)findViewById(R.id.imageWaiting);
        mAnimationWait = (AnimationDrawable)waitImageView.getBackground();

        imageLoading0 = (ImageView)findViewById(R.id.imageLoading0);
        mAnimation0 = AnimationUtils.loadAnimation(this, R.anim.loading0_rotate_anim);
        mAnimation0.setInterpolator(new LinearInterpolator());
        imageLoading1 = (ImageView)findViewById(R.id.imageLoading1);
        mAnimation1 = AnimationUtils.loadAnimation(this, R.anim.loading1_rotate_anim);
        mAnimation1.setInterpolator(new LinearInterpolator());

        imageConnecting = (ImageView)findViewById(R.id.imageConnecting);
        mAnimation = AnimationUtils.loadAnimation(this, R.anim.loading0_rotate_anim);
        mAnimation.setInterpolator(new LinearInterpolator());

        Button btnBack = (Button) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopAnimation();
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        startAnimation();
    }

    private void startAnimation() {
        imageConnecting.startAnimation(mAnimation);

        imageLoading0.startAnimation(mAnimation0);
        imageLoading1.startAnimation(mAnimation1);

        mAnimationWait.start();
    }
    private void stopAnimation() {
        imageConnecting.clearAnimation();

        imageLoading0.clearAnimation();
        imageLoading1.clearAnimation();

        mAnimationWait.stop();
    }
}
